package org.example.cet.demos.controller;


import jakarta.validation.Valid;
import org.example.cet.demos.converter.converter;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.Result;
import org.example.cet.demos.domain.form.UserForm;
import org.example.cet.demos.domain.po.User;
import org.example.cet.demos.domain.query.UserQuery;
import org.example.cet.demos.mapper.UserMapper;
import org.example.cet.demos.service.UserService;
import org.example.cet.demos.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private converter converter;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userLoginService;

    @PostMapping("/add")
    public Result add(@Valid @RequestBody UserForm userForm) {
        User user = converter.ToUser(userForm);
        userService.save(user);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        userService.removeById(id);
        return Result.success();
    }

    @PutMapping("/update/{username}")
    public Result update(@PathVariable String username, @Valid @RequestBody UserForm userForm) {
        User user = converter.ToUser(userForm);
        user.setUsername(username);
        userService.updateById(user);
        return Result.success();
    }

    @GetMapping("/getPage")
    public Result getPage(UserQuery userQuery) {
        PageBean pb = userService.getPage(userQuery);
        return Result.success(pb);
    }

//    @GetMapping("/findMyInf")
//    public Result findMyInf(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer pageSize, String username) {
//        PageBean pageBean = userLoginService.findMyInf(page, pageSize, username);
//        return Result.success(pageBean);
//    }
}
